﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace SPA_Prototype.Model
{
    public enum CUSTOMER_SERACH_METHOD { ALL, NAME, PHONE, ID ,LV };
    public class CustomerManagement
    {
        private DatabaseDataSet _Dataset;
        private DatabaseDataSet.CustomerTableDataTable _CustomerTable;
        private DatabaseDataSet.CustomerTableDataTable _Table_Search;

       

        private DatabaseDataSetTableAdapters.CustomerTableTableAdapter _Adt;
    

      

        // constructor
        public CustomerManagement(DatabaseDataSet Dtset)
        {
            _Dataset = Dtset;
            _CustomerTable = _Dataset.CustomerTable;
      

            _Table_Search = new DatabaseDataSet.CustomerTableDataTable();
            
            _Adt = new DatabaseDataSetTableAdapters.CustomerTableTableAdapter();
       

            LoadDB();
        }

        // Field   
        public int VIPLevel_Account;
        public int VIPLevel_Point;

        public int Count    // Number of customers
        {
            get
            {
                return (_CustomerTable.Rows.Count);
            }
        }

        // methods: LoadDB, UpdateDB, AddNewCustomer, SearchBy, GetSourceForKey, Addpoint for cusomer
        public bool AddPointForCustomer(int Money, string CustomerNameID)
        {
            bool bSuccess = false;
            for (int j = 0; j < Count; j++)
            {
                if (_CustomerTable[j].ID == CustomerNameID)
                {
                    _CustomerTable[j].Account += Money;
                    _CustomerTable[j].Visit_Times++;
                    
                    // check level of customer

                    if(_CustomerTable[j].Level != "VIP")
                    if ((_CustomerTable[j].Account >= VIPLevel_Account) || (_CustomerTable[j].Visit_Times >= VIPLevel_Point))
                        _CustomerTable[j].Level = "VIP";

                    UpdateDB();
                    bSuccess = true;
                    break;
                }
            }
            return bSuccess;
        }

        public void LoadDB()
        {
            _Adt.Fill(_CustomerTable);
        }

        public void UpdateDB()
        {
            try
            {
                _Adt.Update(_CustomerTable);
                MessageBox.Show("Successful !", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void AddNewCustomer(string Id,string Name,string Phone,string LV)
        {
            try
            {
                _CustomerTable.Rows.Add(new object[] { Id, Name, Phone, LV, 0, 0, "" });
            }
            catch(Exception ex)
            {
                MessageBox.Show("Input value is invalid ! \r\n\r\n" + ex.Message,"ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
                return;
            }
            UpdateDB();
        }

        public DatabaseDataSet.CustomerTableDataTable SearchBy(string key, CUSTOMER_SERACH_METHOD type)
        {
            _Table_Search.Clear();
            
            switch(type)
            {
                case CUSTOMER_SERACH_METHOD.NAME:
                        var query = from q in _CustomerTable
                        where q.Name == key        
                        select new { q.ID, q.Name, q.Phone, q.Level, q.Visit_Times, q.Account, q.Notes };
                        foreach (var order in query) _Table_Search.Rows.Add(new object[] { order.ID, order.Name, order.Phone, order.Level, order.Visit_Times, order.Account, order.Notes });
                        break;
                case CUSTOMER_SERACH_METHOD.ID:
                        query = from q in _CustomerTable
                        where q.ID == key
                        select new { q.ID, q.Name, q.Phone, q.Level, q.Visit_Times, q.Account, q.Notes };
                        foreach (var order in query) _Table_Search.Rows.Add(new object[] { order.ID, order.Name, order.Phone, order.Level, order.Visit_Times, order.Account, order.Notes });
                        break;
                case CUSTOMER_SERACH_METHOD.PHONE:
                        query = from q in _CustomerTable
                        where q.Phone == key
                        select new { q.ID, q.Name, q.Phone, q.Level, q.Visit_Times, q.Account, q.Notes };
                        foreach (var order in query) _Table_Search.Rows.Add(new object[] { order.ID, order.Name, order.Phone, order.Level, order.Visit_Times, order.Account, order.Notes });
                        break;
                case CUSTOMER_SERACH_METHOD.ALL:
                        return _CustomerTable;
             }
          
            return _Table_Search;
        }

        public AutoCompleteStringCollection GetSourceForKey(CUSTOMER_SERACH_METHOD type)
        {
            AutoCompleteStringCollection a = new AutoCompleteStringCollection();
            switch (type)
            {
                case CUSTOMER_SERACH_METHOD.ID:
                    for (int j = 0; j < Count; j++) a.Add(_CustomerTable[j].ID);
                    break;
                case CUSTOMER_SERACH_METHOD.NAME:
                    for (int j = 0; j < Count; j++) a.Add(_CustomerTable[j].Name);
                    break;
                case CUSTOMER_SERACH_METHOD.PHONE:
                    for (int j = 0; j < Count; j++) a.Add(_CustomerTable[j].Phone);
                    break;
                case CUSTOMER_SERACH_METHOD.LV:
                    for (int j = 0; j < Count; j++) a.Add(_CustomerTable[j].Level);
                    break;
            }

            return a;
        }     
    }

}
